package com.dingreading.cloud.web.controller;

import com.dingreading.cloud.common.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

@RestController
@RequestMapping("/front/imageRotation")
@Api(tags = "销售活码")
public class ImageRotationController {

    private static final List<ImageInfo> IMAGE_INFOS = Arrays.asList(
            new ImageInfo("https://dingding-public.oss-cn-beijing.aliyuncs.com/saler/zhouying.jpg", "周莹"),
            new ImageInfo("https://dingding-public.oss-cn-beijing.aliyuncs.com/saler/zhanghai.png", "张海"),
            new ImageInfo("https://dingding-public.oss-cn-beijing.aliyuncs.com/saler/gaotingting.png", "高婷婷"),
            new ImageInfo("https://dingding-public.oss-cn-beijing.aliyuncs.com/saler/zhaokangfei.png", "赵康飞"),
            new ImageInfo("https://dingding-public.oss-cn-beijing.aliyuncs.com/saler/liqinglian.jpg", "李青莲"),
            new ImageInfo("https://dingding-public.oss-cn-beijing.aliyuncs.com/saler/lixuzhao.jpg", "李旭照"),
            new ImageInfo("https://dingding-public.oss-cn-beijing.aliyuncs.com/saler/huwei.png", "胡伟"),
            new ImageInfo("https://dingding-public.oss-cn-beijing.aliyuncs.com/saler/beibei.jpg", "北北"),
            new ImageInfo("https://dingding-public.oss-cn-beijing.aliyuncs.com/saler/junjun.jpg", "君君")
    );
    private static final AtomicLong counter = new AtomicLong (0);


    @ApiOperation(value = "获取二维码")
    @PostMapping(value = "/getNextImage", produces = "application/json; charset=UTF-8")
    public R<Object> getNextImage() {

        long currentVal = counter.getAndIncrement();
        int listSize = IMAGE_INFOS.size();

        int currentIndex = (int) (Long.remainderUnsigned(currentVal, listSize));

        ImageInfo imageInfo = IMAGE_INFOS.get(currentIndex);

        return R.ok(imageInfo);
    }

    static class ImageInfo {
        private String url;
        private String text;

        public ImageInfo(String url, String text) {
            this.url = url;
            this.text = text;
        }
        public String getUrl() {
            return url;
        }

        public String getText() {
            return text;
        }
    }
}
